package com.bringway.p2p.controller;

import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.p2p.service.DqtzxiService;
/**
 * 
 * @author qjy
 * @version 创建时间：2016年06月14日 上午13:15:42
 * @功能：异常查询
 */
@Controller
@RequestMapping("/dqtzxi")
public class DqtzxiController  extends JsonController {
	
	@Autowired
	private DqtzxiService dqtzxiService;
	/**
	 * 
	 * @param request
	 * @param response 查询数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryDqtzxiList")
	public ModelAndView queryDqtzxiList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String yhm= RequestUtils.getStringParameter(request, "yhm");
		String sjhm= RequestUtils.getStringParameter(request, "sjhm");
		String ltype= RequestUtils.getStringParameter(request, "ltype");
		String lyb= RequestUtils.getStringParameter(request, "lyb");
		params.put("start", start);
		params.put("limit", limit);
		params.put("lyb", lyb);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("yhm", yhm);
		params.put("sjhm", sjhm);
		params.put("ltype", ltype);
		List<Map<String, Object>> list = dqtzxiService.queryXiList(params);
		int count = dqtzxiService.gettDqtzxiCount(params);
		String sumJe=dqtzxiService.getDqtzxiSum(params);
		DecimalFormat df = new DecimalFormat("#,###");
		result.put("data", list);	
		result.put("total", count);
		result.put("sumJe", null==sumJe?"":df.format(new Double(sumJe)));
		if (null!=page) {
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
			//合计行 数据 开始
			Map<String, Object> map=new HashMap<>();
			map.put("USER_NAME", "合计:");//key是字段名
			map.put("DQ_JE", null==sumJe?"":df.format(new Double(sumJe)));//key是字段名
			result.put("userdata", map);
			//合计行 数据 结束
		}
		serialize(request,response,result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 queryDqtzxx  
	 * @功能 TODO 获取投资来源信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryDqtzxx")
	public ModelAndView queryDqtzxx(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String tzrxxid= RequestUtils.getStringParameter(request, "tzrxxid");
		String tzly= RequestUtils.getStringParameter(request, "tzly");
		params.put("tzrxxid", tzrxxid);
		params.put("tzly", tzly);
		Map<String, Object> tzrxxInfo=dqtzxiService.queryDqtzxx(params);
		result.put("data", tzrxxInfo);
		serialize(request,response,result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 exportDqtzxiExcelMethod  
	 * @功能 TODO 点券来源下载
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("exportDqtzxiExcelMethod")
	public ModelAndView exportDqtzxiExcelMethod(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JSONObject params = new JSONObject();
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String yhm= RequestUtils.getStringParameter(request, "yhm");
		String sjhm= RequestUtils.getStringParameter(request, "sjhm");
		String ltype= RequestUtils.getStringParameter(request, "ltype");
		String lyb= RequestUtils.getStringParameter(request, "lyb");
		params.put("lyb", lyb);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("yhm", yhm);
		params.put("sjhm", sjhm);
		params.put("ltype", ltype);
		List<Map<String, Object>> list=dqtzxiService.queryXiList(params);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.getDqtzxiExportSearchResultData(list);
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(DateUtils.getCurrentDataStr() + "点券来源明细");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = DateUtils.getCurrentDataStr() + "点券来源明细.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");

		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
}
